﻿<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"
    xmlns:imaging="http://blogs.msdn.com/synergist/imaging"
    xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math"
    xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture"
    xmlns:pkg="http://schemas.microsoft.com/office/2006/xmlPackage"
    xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
    xmlns:rels="http://schemas.openxmlformats.org/package/2006/relationships"
    xmlns:util="urn:utilities"
    xmlns:v="urn:schemas-microsoft-com:vml"
    xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
    xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    extension-element-prefixes="imaging util"
    exclude-result-prefixes="a imaging pic pkg r rels util v w wp xsl">

  <xsl:import href="OMML2Xaml.xslt"/>

  <!-- We don't indent the XAML because the XAML text content model treats newlines as spaces-->
  <xsl:output encoding="utf-8" indent="no" omit-xml-declaration="no" method="xml" standalone="yes" media-type="application/x-silverlight-2"/>

  <xsl:param name="IsOptimalParagraphEnabled" select="'True'"/>
  <xsl:param name="IsHyphenationEnabled" select="'True'"/>
  <xsl:param name="IsColumnWidthFlexible" select="'False'"/>
  <xsl:param name="ColumnWidth" select="'Auto'"/>
  <xsl:param name="PagePadding" select="0"/>

  <xsl:template match="/pkg:package">
    <xsl:comment>This Silverlight 5 XAML was generated by Michael S. Scherotter's Microsoft Word to Xaml Generator: http://www.codeplex.com/Word2007ToXaml</xsl:comment>
    <xsl:apply-templates select="pkg:part[@pkg:name='/word/document.xml']/pkg:xmlData/w:document">
      <xsl:with-param name="styles" select="pkg:part[@pkg:name='/word/styles.xml']/pkg:xmlData/w:styles"/>
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="m:oMathPara">
    <InlineUIContainer>
      <xsl:apply-templates select="m:oMath"/>
    </InlineUIContainer>
  </xsl:template>

  <xsl:variable name="PtToPixels" select="96 div 72"/>

  <xsl:template match="w:document">
    <xsl:param name="styles"/>
    <RichTextBox>
      <!--
      <RichTextBox.Resources>
        <xsl:apply-templates select="$styles/w:style">
          <xsl:with-param name="styles" select="$styles"/>
        </xsl:apply-templates>
      </RichTextBox.Resources>
      -->
      <xsl:apply-templates select="w:body/*"/>
    </RichTextBox>
  </xsl:template>

  <xsl:template match="w:style">
    <xsl:param name="styles"/>
    <xsl:comment>
      <xsl:value-of select="concat(w:name/@w:val, ' Style')"/>
    </xsl:comment>
    <xsl:variable name="basedOn" select="w:basedOn/@w:val"/>

    <Style x:Key="{@w:styleId}" TargetType="Paragraph">
      <xsl:if test="$basedOn">
        <xsl:attribute name="BasedOn">
          {StaticResource <xsl:value-of select="$basedOn"/>}
        </xsl:attribute>
      </xsl:if>

      <!--<Setter Property="TextWrapping" Value="Wrap"/>-->
      <!--<Setter Property="Margin" Value="0,0,0,5"/>-->
      <xsl:if test="w:rPr/w:b">
        <Setter Property="FontWeight" Value="Bold"/>
      </xsl:if>
      <xsl:if test="w:rPr/w:color/@w:val">
        <Setter Property="Foreground" Value="#{w:rPr/w:color/@w:val}"/>
      </xsl:if>
      <xsl:choose>
        <xsl:when test="w:rPr/w:sz/@w:val">
          <Setter Property="FontSize"   Value="{$PtToPixels * w:rPr/w:sz/@w:val div 2}"/>
        </xsl:when>
        <xsl:otherwise>
          <Setter Property="FontSize"   Value="{$PtToPixels * $styles/w:docDefaults/w:rPrDefault/w:rPr/w:sz/@w:val div 2}"/>
        </xsl:otherwise>
      </xsl:choose>
      <!-- Font Family -->
      <xsl:choose>
        <xsl:when test="w:rPr/w:rFonts/@w:ascii">
          <Setter Property="FontFamily" Value="{w:rPr/w:rFonts/@w:ascii}"/>
        </xsl:when>
        <xsl:when test="w:rPr/w:rFonts/@w:asciiTheme='majorHAnsi'">
          <Setter Property="FontFamily" Value="{//a:theme/a:themeElements/a:fontScheme/a:majorFont/a:latin/@typeface}"/>
        </xsl:when>
        <xsl:otherwise>
          <Setter Property="FontFamily" Value="{//a:theme/a:themeElements/a:fontScheme/a:minorFont/a:latin/@typeface}"/>
        </xsl:otherwise>
      </xsl:choose>
      <xsl:if test="w:rPr/w:i">
        <Setter Property="FontStyle" Value="Italic"/>
      </xsl:if>

      <!-- Text Alignment -->
      <xsl:if test="@w:type='paragraph'">
        <xsl:choose>
          <xsl:when test="w:pPr/w:jc/@w:val = 'left'">
            <Setter Property="TextAlignment" Value="Left"/>
          </xsl:when>
          <xsl:when test="w:pPr/w:jc/@w:val = 'center'">
            <Setter Property="TextAlignment" Value="Center"/>
          </xsl:when>
          <xsl:when test="w:pPr/w:jc/@w:val = 'right'">
            <Setter Property="TextAlignment" Value="Right"/>
          </xsl:when>
          <xsl:otherwise>
            <Setter Property="TextAlignment" Value="Left"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:if>

    </Style>
  </xsl:template>

  <xsl:template match="@w:val">
    <xsl:attribute name="Style">
      <xsl:value-of select="concat('{StaticResource ', ., '}')"/>
    </xsl:attribute>
  </xsl:template>

  <xsl:template name="Style">
    <xsl:param name="style"/>
    <xsl:if test="$style/w:i">
      <xsl:attribute name="FontStyle">Italic</xsl:attribute>
    </xsl:if>
    <xsl:if test="$style/w:b">
      <xsl:attribute name="FontWeight">Bold</xsl:attribute>
    </xsl:if>
    <xsl:if test="$style/w:color/@w:val and $style/w:color/@w:val != 'auto'">
      <xsl:attribute name="Foreground">
        <xsl:value-of select="concat('#', $style/w:color/@w:val)"/>
      </xsl:attribute>
    </xsl:if>
    <xsl:if test="$style/w:sz/@w:val">
      <xsl:attribute name="FontSize">
        <xsl:value-of select="$PtToPixels * $style/w:sz/@w:val div 2"/>
      </xsl:attribute>
    </xsl:if>
    <xsl:if test="$style/w:rFonts/@w:ascii">
      <xsl:attribute name="FontFamily">
        <xsl:value-of select="$style/w:rFonts/@w:ascii"/>
      </xsl:attribute>
    </xsl:if>
  </xsl:template>

  <xsl:template match="w:p">
    <xsl:variable name="styleId">
      <xsl:choose>
        <xsl:when test="w:pPr/w:pStyle/@w:val">
          <xsl:value-of select="w:pPr/w:pStyle/@w:val"/>
        </xsl:when>
        <xsl:otherwise>
          <!-- The default paragraph style -->
          <xsl:value-of select="//w:style[@w:type='paragraph' and @w:default=1]/@w:styleId"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <Paragraph>
      <xsl:apply-templates select="*"/>
    </Paragraph>
  </xsl:template>

  <!-- Break -->
  <xsl:template match="w:br">
    <LineBreak/>
  </xsl:template>

  <!-- Run -->
  <xsl:template match="w:r">
    <xsl:apply-templates select="*">
      <xsl:with-param name="style" select="w:rPr"/>
    </xsl:apply-templates>
  </xsl:template>

  <!-- Run with a picture-->
  <xsl:template match="w:r[w:pict]">
    <InlineUIContainer>
      <xsl:apply-templates select="*"/>
    </InlineUIContainer>
  </xsl:template>

  <xsl:template match="w:pict">
    <ViewBox>
      <xsl:apply-templates select="*"/>
    </ViewBox>
  </xsl:template>

  <!-- Embedded Image -->
  <xsl:template match="w:drawing[wp:inline/a:graphic/a:graphicData/pic:pic/pic:blipFill/a:blip/@r:embed]">
    <xsl:if test="element-available('imaging')">
      <xsl:variable name="id" select="wp:inline/a:graphic/a:graphicData/pic:pic/pic:blipFill/a:blip/@r:embed"/>
      <xsl:variable name="target" select="//rels:Relationship[@Id=$id and @Type='http://schemas.openxmlformats.org/officeDocument/2006/relationships/image']/@Target"/>
      <xsl:variable name="packageName" select="concat('/word/', $target)"/>
      <xsl:variable name="data" select="string(//pkg:part[@pkg:name=$packageName]/pkg:binaryData)"/>
      <xsl:comment>
        Embedded Image: <xsl:value-of select="$id"/>,
        Package Name: <xsl:value-of select="$packageName"/>
      </xsl:comment>

      <xsl:variable name="source" select="imaging:SaveImage($target, $data)"/>

      <xsl:if test="string-length($source)">
        <InlineUIContainer>
          <Image Source="{$source}">
            <xsl:if test="wp:inline/wp:extent/@cx">
              <xsl:attribute name="Width">
                <xsl:value-of select="wp:inline/wp:extent/@cx div 9525"/>
              </xsl:attribute>
            </xsl:if>
            <xsl:if test="wp:inline/wp:extent/@cy">
              <xsl:attribute name="Height">
                <xsl:value-of select="wp:inline/wp:extent/@cy div 9525"/>
              </xsl:attribute>
            </xsl:if>
          </Image>
        </InlineUIContainer>
      </xsl:if>
    </xsl:if>
  </xsl:template>

  <!-- Linked Image -->
  <xsl:template match="w:drawing[wp:inline/a:graphic/a:graphicData/pic:pic/pic:blipFill/a:blip/@r:link]">
    <xsl:if test="element-available('imaging')">
      <xsl:variable name="id" select="wp:inline/a:graphic/a:graphicData/pic:pic/pic:blipFill/a:blip/@r:link"/>
      <xsl:variable name="target" select="//rels:Relationship[@Id=$id and @Type='http://schemas.openxmlformats.org/officeDocument/2006/relationships/image']/@Target"/>

      <xsl:variable name="source" select="imaging:CopyImage($target)"/>

      <xsl:comment>
        Linked Image: <xsl:value-of select="$target"/>,
      </xsl:comment>

      <xsl:if test="string-length($source)">
        <Image Source="{$source}"/>
      </xsl:if>
    </xsl:if>
  </xsl:template>

  <!--
  <xsl:template match="w:p[w:pPr/w:pStyle/@w:val='ListParagraph' and preceding-sibling::w:p[1]/w:pPr/w:pStyle/@w:val='ListParagraph']">
    <xsl:comment>List Paragraph</xsl:comment>
  </xsl:template>
-->
  <!--
  <xsl:template match="w:p[w:pPr/w:pStyle/@w:val='ListParagraph']">
    <TextBlock>
      <xsl:call-template name="ListItem">
        <xsl:with-param name="item" select="."/>
      </xsl:call-template>
    </TextBlock>
  </xsl:template>
  -->

  <xsl:template name="ListItem">
    <xsl:param name="item"/>

    <Run>
      <xsl:variable name="styleId" select="$item/w:pPr/w:pStyle/@w:val"/>
      <xsl:variable name="style" select="//w:style[@w:styleId=$styleId]"/>
      <xsl:call-template name="Style">
        <xsl:with-param name="style" select="$style"/>
      </xsl:call-template>
      <xsl:apply-templates select="$item/*"/>
    </Run>
    <xsl:variable name="following" select="following::w:p[1]"/>
    <xsl:if test="$following/w:pPr/w:pStyle/w:val='ListParagraph'">
      <xsl:call-template name="ListItem">
        <xsl:with-param name="item" select="$following"/>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>

  <xsl:template match="w:r[w:instrText]"/>

  <!--
  <xsl:template name="TextStyle">
    <xsl:param name="style"/>
    <xsl:variable name="elementName">
      <xsl:choose>
        <xsl:when test="local-name($style) = 'i'">
          <xsl:text>Italic</xsl:text>
        </xsl:when>
        <xsl:when test="local-name($style) = 'b'">
          <xsl:text>Bold</xsl:text>
        </xsl:when>
      </xsl:choose>
    </xsl:variable>

    <xsl:choose>
      <xsl:when test="string-length($elementName)">
          <xsl:element name="{$elementName}">
            <xsl:call-template name="TextStyle">
              <xsl:with-param name="style" select="$style/following-sibling::node()[1]"/>
            </xsl:call-template>
          </xsl:element>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates select="*"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  -->

  <xsl:template match="w:t">
    <xsl:param name="style"/>
    <Run Text="{util:CleanXaml(text())}">
      <xsl:call-template name="Style">
        <xsl:with-param name="style" select="$style"/>
      </xsl:call-template>
    </Run>
  </xsl:template>

  <xsl:template match="w:tbl">
    <xsl:comment>Inline tables are not supported with Silverlight.</xsl:comment>
  </xsl:template>

</xsl:stylesheet>